package com.mooc.yohoshop.ui.home;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.entity.BannerEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.BrandEntity;
import com.mooc.yohoshop.entity.CategoryEntity;
import com.mooc.yohoshop.entity.GoodsEntity;
import com.mooc.yohoshop.entity.LoadGoodsEntity;
import com.mooc.yohoshop.ui.base.BaseLazyFragment;
import com.mooc.yohoshop.ui.detail.GoodsDetailActivity;
import com.mooc.yohoshop.widget.HomeBrandDivider;
import com.mooc.yohoshop.widget.HomeGoodsDivider;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import java.lang.ref.WeakReference;
import java.util.HashMap;

public class HomeFragment extends BaseLazyFragment<HomeRepo> implements OnRefreshLoadMoreListener, OnItemClickListener {

    private View bannerHeader;
    private View brandHeader;
    private View categoryHeader;
    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView goodsRv;
    public RecyclerView home_category_rv;
    private HomeCategoryAdapter homeCategoryAdapter;
    private Banner banner;
    private RecyclerView brandRv;
    private RecyclerView categoryRv;
    public RelativeLayout home_bottem_relative;
    private HomeCategoryAdapter categoryAdapter;
    private HomeGoodsAdapter goodsAdapter;
    private HomeBrandAdapter brandAdapter;
    private HomeBannerAdapter bannerAdapter;
    public int bannerHeight;//banner的高度
    public int talHeight;//滑动的总高度
    public int brandHeight;//分类的高度
    //选中得分类id
    private long typeId = 0;
    private int page = 1;

    @Override
    protected int layout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void init() {
        initBanner();
        initBrand();
        initCategory();
        initHomeCategory();
        initGoods();
        home_bottem_relative = f(R.id.home_bottem_relative);
        smartRefreshLayout = f(R.id.home_pull_layout);
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        smartRefreshLayout.setOnRefreshLoadMoreListener(this);
    }

    private void initBanner(){
        bannerHeader = LayoutInflater.from(getContext()).inflate(R.layout.layout_home_banner,null,false);
        banner = bannerHeader.findViewById(R.id.home_banner_top);
        bannerHeader.measure(0,0);
        bannerHeight = bannerHeader.getMeasuredHeight();
        banner.clearFocus();
        banner.setFocusable(false);
        banner.addBannerLifecycleObserver(this);
    }

    private void initBrand(){
        brandHeader = LayoutInflater.from(getContext()).inflate(R.layout.layout_home_brand,null,false);
        brandRv = brandHeader.findViewById(R.id.home_brand);
        brandRv.addItemDecoration(new HomeBrandDivider(getContext()));
        GridLayoutManager brandManager = new GridLayoutManager(getContext(),6);
        brandManager.setOrientation(GridLayoutManager.VERTICAL);
        brandRv.setLayoutManager(brandManager);
        brandAdapter = new HomeBrandAdapter();
        brandRv.setAdapter(brandAdapter);
        brandHeader.measure(0,0);
        brandHeight = brandHeader.getMeasuredHeight();
    }

    private void initCategory(){
        categoryHeader = LayoutInflater.from(getContext()).inflate(R.layout.layout_home_category,null,false);
        categoryRv = categoryHeader.findViewById(R.id.home_category_top);
        LinearLayoutManager categoryManager = new LinearLayoutManager(getContext());
        categoryManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        categoryRv.setLayoutManager(categoryManager);
        categoryAdapter = new HomeCategoryAdapter();
        categoryAdapter.setOnItemClickListener(this);
        categoryRv.setAdapter(categoryAdapter);
    }

    private void initHomeCategory(){
        home_category_rv = f(R.id.home_category_rv);
        homeCategoryAdapter = new HomeCategoryAdapter();
        homeCategoryAdapter.setOnItemClickListener(this);
        LinearLayoutManager homeCategoryManager = new LinearLayoutManager(getContext());
        homeCategoryManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        home_category_rv.setLayoutManager(homeCategoryManager);
        home_category_rv.setAdapter(homeCategoryAdapter);
    }

    private void initGoods(){
        goodsRv = f(R.id.home_goods_rv);
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        goodsRv.addItemDecoration(new HomeGoodsDivider(getContext()));
        goodsRv.setLayoutManager(manager);
        goodsRv.addOnScrollListener(new GoodsOnScrollListener(this));
        goodsAdapter = new HomeGoodsAdapter();
        goodsAdapter.setOnItemClickListener(this);
        goodsAdapter.addHeaderView(bannerHeader);
        goodsAdapter.addHeaderView(brandHeader);
        goodsAdapter.addHeaderView(categoryHeader);
        goodsRv.setAdapter(goodsAdapter);
    }

    @Override
    protected void lazyInit() {
        HashMap<String,Object>map = new HashMap<>();
        map.put("menuId","1");
        request(repo.requestAll(map));
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof BannerEntity){
            BannerEntity bannerEntity = (BannerEntity) baseEntity;
            bannerAdapter = new HomeBannerAdapter(bannerEntity.data);
            banner.setAdapter(bannerAdapter)
                    .setOrientation(Banner.HORIZONTAL)
                    .start();
        }else if (baseEntity instanceof CategoryEntity){
            CategoryEntity categoryEntity = (CategoryEntity) baseEntity;
            categoryAdapter.setNewInstance(categoryEntity.data);
            homeCategoryAdapter.setNewInstance(categoryEntity.data);
            typeId = categoryEntity.data.get(0).category_id;
        }else if (baseEntity instanceof BrandEntity){
            BrandEntity brandEntity = (BrandEntity) baseEntity;
            brandAdapter.setNewInstance(brandEntity.data);
        }else if (baseEntity instanceof GoodsEntity){
            GoodsEntity goodsEntity = (GoodsEntity) baseEntity;
            goodsAdapter.setNewInstance(goodsEntity.data);
        }else if (baseEntity instanceof LoadGoodsEntity){
            LoadGoodsEntity loadGoodsEntity = (LoadGoodsEntity) baseEntity;
            goodsAdapter.addData(loadGoodsEntity.data);
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        page += 1;
        HashMap<String,Object>requestMap = new HashMap<>();
        requestMap.put("page",page);
        requestMap.put("typeId",typeId);
        request(repo.requestMoreGoods(requestMap));
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        page = 1;
        HashMap<String,Object>requestMap = new HashMap<>();
        requestMap.put("page",page);
        requestMap.put("typeId",typeId);
        request(repo.requestGoods(requestMap));
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof HomeCategoryAdapter){
            typeId =  categoryAdapter.getItem(position).category_id;
            page = 1;
            HashMap<String,Object>requestMap = new HashMap<>();
            requestMap.put("page",page);
            requestMap.put("typeId",typeId);
            request(repo.requestGoods(requestMap));
        }else if (adapter instanceof HomeGoodsAdapter){
            typeId = goodsAdapter.getItem(position).goods_id;
            Intent intent = new Intent(getContext(),GoodsDetailActivity.class);
            intent.putExtra("goodsId",goodsAdapter.getItem(position).goods_id);
            intent.putExtra("typeId",typeId);
            startActivity(intent);
        }

    }

    public static class GoodsOnScrollListener extends RecyclerView.OnScrollListener{
        private WeakReference<HomeFragment>weakReference;
        public GoodsOnScrollListener(HomeFragment fragment){
            weakReference = new WeakReference(fragment);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            weakReference.get().talHeight += dy;
            if (weakReference.get().talHeight >= weakReference.get().bannerHeight &&
                    weakReference.get().talHeight<(weakReference.get().bannerHeight+weakReference.get().brandHeight)){
                weakReference.get().home_category_rv.setVisibility(View.VISIBLE);
                weakReference.get().home_bottem_relative.setAlpha(1);
            }else if (weakReference.get().talHeight >=(weakReference.get().bannerHeight+weakReference.get().brandHeight)){

            }else {
                weakReference.get().home_category_rv.setVisibility(View.GONE);
                weakReference.get().home_bottem_relative.setAlpha(0);
            }
        }
    }

}
